### Save code in a file "demo.r" and run with 2 processors by
### SHELL> mpiexec -np 2 Rscript demo.r
### Initial.
suppressMessages(library(pbdMPI, quietly = TRUE))
init()
.comm.size <- comm.size()
.comm.rank <- comm.rank()
### Example for pbdApply.
N <- 100
x <- matrix((1:N) + N * .comm.rank, ncol = 10)
y <- pbdApply(x, 1, sum, pbd.mode = "mw")
comm.print(y)
y <- pbdApply(x, 1, sum, pbd.mode = "spmd")
comm.print(y)
y <- pbdApply(x, 1, sum, pbd.mode = "dist")
comm.print(y)
### Example for pbdApply for 3D array.
N <- 60
x <- array((1:N) + N * .comm.rank, c(3, 4, 5))
dimnames(x) <- list(lat = paste("lat", 1:3, sep = ""),
lon = paste("lon", 1:4, sep = ""),
time = paste("time", 1:5, sep = ""))
comm.print(x[,, 1:2])
y <- pbdApply(x, c(1, 2), sum, pbd.mode = "mw")
comm.print(y)
y <- pbdApply(x, c(1, 2), sum, pbd.mode = "spmd")
comm.print(y)
y <- pbdApply(x, c(1, 2), sum, pbd.mode = "dist")
comm.print(y)
### Example for pbdLapply.
N <- 100
x <- split((1:N) + N * .comm.rank, rep(1:10, each = 10))
y <- pbdLapply(x, sum, pbd.mode = "mw")
comm.print(unlist(y))
y <- pbdLapply(x, sum, pbd.mode = "spmd")
comm.print(unlist(y))
y <- pbdLapply(x, sum, pbd.mode = "dist")
comm.print(unlist(y))
### Finish.
finalize()
Run the code above in your browser using DataLab